#!/usr/bin/env python

###############################################################################
# Author: Greg Ryman                                                          #
# Name: Kidspaint                                                             #
# Version: 0.2                                                                #
# Description: A simple kids paintint program.                                #
# Url: http://code.google.com/p/kidspaint/                                    #
# Copyright 2007                                                              #
#                                                                             #
#                                                                             #
#    This program is free software: you can redistribute it and/or modify     #
#    it under the terms of the GNU General Public License as published by     #
#    the Free Software Foundation, either version 3 of the License, or        #
#    (at your option) any later version.                                      # 
#                                                                             #
#    This program is distributed in the hope that it will be useful,          #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of           #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            #
#    GNU General Public License for more details.                             #
#                                                                             #
#    You should have received a copy of the GNU General Public License        #
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.    #
#                                                                             #
###############################################################################

import pygame

def checkKeys(myData):
    
    (event, background, drawColor, lineWidth, keepGoing) = myData
    
    if event.key == pygame.K_q:
        # Quit
        keepGoing = False
    elif event.key == pygame.K_c:
        # Clear
        background.fill((255, 255, 255))
    elif event.key == pygame.K_s:
        # Save
        pygame.image.save(background, "painting.bmp")
    elif event.key == pygame.K_l:
        # Load
        background = pygame.image.load("painting.bmp")
    elif event.key == pygame.K_r:
        # Red
        drawColor = (255, 0, 0)
    elif event.key == pygame.K_g:
        # Green
        drawColor = (0, 255, 0)
    elif event.key == pygame.K_w:
        # White
        drawColor = (255, 255, 255)
    elif event.key == pygame.K_b:
        # Blue
        drawColor = (0, 0, 255)
    elif event.key == pygame.K_k:
        # Black
        drawColor = (0, 0, 0)
    elif event.key == pygame.K_y:
        # Yellow
        drawColor = (255, 255, 0)
    elif event.key == pygame.K_p:
        # Pink
        drawColor = (255, 20, 147)
    elif event.key == pygame.K_o:
        # Orange
        drawColor = (255, 69, 0)
    elif event.key == pygame.K_v:
        # Violet
        drawColor = (148, 0, 211)
    elif event.key == pygame.K_t:
        # Teal
        drawColor = (0, 250, 154)
    elif event.key == pygame.K_u:
        # Ugly
        drawColor = (139, 69, 19)
    
    # Line widths
    elif event.key == pygame.K_1:
        lineWidth = 1
    elif event.key == pygame.K_2:
        lineWidth = 2
    elif event.key == pygame.K_3:
        lineWidth = 3
    elif event.key == pygame.K_4:
        lineWidth = 4
    elif event.key == pygame.K_5:
        lineWidth = 5
    elif event.key == pygame.K_6:
        lineWidth = 6
    elif event.key == pygame.K_7:
        lineWidth = 7
    elif event.key == pygame.K_8:
        lineWidth = 8
    elif event.key == pygame.K_9:
        lineWidth = 9
        
    # Return all values
    myData = (event, background, drawColor, lineWidth, keepGoing)
    return myData

def showStats(drawColor, lineWidth):
    myFont = pygame.font.SysFont("none", 20)
    stats = "color: %s, width: %d" % (drawColor, lineWidth)
    statSurf = myFont.render(stats, 1, (drawColor))
    return statSurf

def main():
    pygame.init()
    screen = pygame.display.set_mode((800, 600))
    pygame.display.set_caption("Paint: (r)ed, (g)reen, (b)lue, (w)hite, blac(k), (1-9) width, (c)lear, (s)ave, (l)oad, (q)uit")
    
    background = pygame.Surface(screen.get_size())
    background.fill((255, 255, 255))
    
    clock = pygame.time.Clock()
    keepGoing = True
    lineStart = (0, 0)
    drawColor = (0, 0, 0)
    lineWidth = 3
    
    while keepGoing:
        clock.tick(300)
        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                keepGoing = False
            elif event.type == pygame.MOUSEMOTION:
                lineEnd = pygame.mouse.get_pos()
                if pygame.mouse.get_pressed() == (1, 0, 0):
                    pygame.draw.line(background, drawColor, lineStart, lineEnd, lineWidth)
                lineStart = lineEnd
            elif event.type == pygame.KEYDOWN:
                myData = (event, background, drawColor, lineWidth, keepGoing)
                myData = checkKeys(myData)
                (event, background, drawColor, lineWidth, keepGoing) = myData
                
        screen.blit(background, (0, 0))
        myLabel = showStats(drawColor, lineWidth)
        screen.blit(myLabel, (550, 550))
        pygame.display.flip()
        
if __name__ == "__main__":
    main()
